//
//  WBStatusCell.m
//  WeiBoOC
//
//  Created by 韩菲菲 on 16/8/13.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

#import "WBStatusCell.h"
#import "WBTopView.h"
#import <Masonry.h>
#import "WBBottomView.h"
#import "WBMiddleView.h"

@interface WBStatusCell ()

//顶部视图(原创微博)
@property (nonatomic , strong) WBTopView *topView;

//中间视图（转发微博）
@property (nonatomic , strong) WBMiddleView *middleView;

//底部视图(底部按钮)
@property (nonatomic , strong) WBBottomView *bottomView;

@property (nonatomic , strong) MASConstraint *bottomViewTopConstraint;

@end

@implementation WBStatusCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        
        //设置UI
        [self setupUI];
        
    }
    
    return self;
    
    
}

//重写set方法 外界传递过来数据的时候 给控件赋值
- (void)setStatusViewModel:(WBStatusViewModel *)statusViewModel
{
    _statusViewModel = statusViewModel;
    
    //给topview里面传递数据
    self.topView.statusViewModel = statusViewModel;
    
    //给middleView传递数据
    //判断有没有转发微博
    //卸载约束
    [self.bottomViewTopConstraint uninstall];
    if(statusViewModel.statusModel.retweeted_status == nil)
    {
        //没有转发微博
        //更新约束
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
           
           self.bottomViewTopConstraint = make.top.mas_equalTo(self.topView.mas_bottom);
        }];
        //隐藏转发微博
        self.middleView.hidden = YES;
    }else
    {
        //有转发微博
        //赋值
        self.middleView.statusViewModel = statusViewModel;
        //更新约束
        [self.bottomView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            self.bottomViewTopConstraint = make.top.mas_equalTo(self.middleView.mas_bottom);
        }];
        //显示转发微博
        self.middleView.hidden = NO;
    }
    
    
    //给底部视图赋值
    self.bottomView.statusViewModel = statusViewModel;

}


//懒加载topview
- (WBTopView *)topView
{
    if(!_topView)
    {
        _topView = [[WBTopView alloc] init];
    }
    return _topView;
}

- (WBMiddleView *)middleView
{
    
    if(!_middleView)
    {
        _middleView = [[WBMiddleView alloc] init];
    }

    return _middleView;
}

- (WBBottomView *)bottomView
{
    if(!_bottomView)
    {
    
        _bottomView = [[WBBottomView alloc] init];
    }
    
    return _bottomView;

}

//设置UI
- (void)setupUI
{
    
    self.backgroundColor = [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1];
    //添加控件 设置约束
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.middleView];
    [self.contentView addSubview:self.bottomView];
    //设置约束
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(8);
        
    }];
    //转发微博
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.leading.trailing.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.topView.mas_bottom);
        //设置高度 该高度已经在middle内部设置好了
        //make.height.mas_equalTo(200);
        
    }];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.mas_equalTo(self.contentView);
        make.height.mas_equalTo(35);
        self.bottomViewTopConstraint = make.top.mas_equalTo(self.middleView.mas_bottom);
    }];
    
    //自动设置行高
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.bottomView);
    }];
  
}
@end
